import 'package:dio/dio.dart';
import 'package:flutter_base_library/http/interceptor/LogInterceptor.dart';
import 'package:flutter_base_library/http/interceptor/TokenInterceptor.dart';

class HttpDioConfig {
  int connectTimeout = 15000;
  int receiveTimeout = 15000;

  int successCode = 200;
  int tokenInvalidCode = 401;

  String errorMessage = "请求失败,请稍后再试";
  String baseUrl = '';

  bool isNeedProxy = false;
  String proxyHost = "";
  Map<String, dynamic> headers;

  InterceptorsWrapper headerInterceptor =
      new InterceptorsWrapper(onRequest: (RequestOptions options) {
    //
    // if (options.path == "xxx") {
    //   //可以通过不同接口进行 全局配置,不用在
    //   options.connectTimeout = 30000;
    //   options.receiveTimeout = 30000;
    // }
    return options;
  });

  InterceptorsWrapper tokenInterceptor;

  InterceptorsWrapper logInterceptor;

  List<InterceptorsWrapper> interceptors = [];

  HttpDioConfig({
    int connectTimeout,
    int receiveTimeout,
    int successCode,
    int tokenInvalidCode,
    String errorMessage,
    String baseUrl,
    bool isNeedProxy,
    String proxyHost,
    Map<String, dynamic> headers,
    InterceptorsWrapper headerInterceptor,
    InterceptorsWrapper tokenInterceptor,
    InterceptorsWrapper logInterceptor,
    List<InterceptorsWrapper> interceptors,
  }) {
    this.connectTimeout =
        connectTimeout == 0 ? this.connectTimeout : connectTimeout;
    this.receiveTimeout =
        receiveTimeout == 0 ? this.receiveTimeout : connectTimeout;
    this.successCode = successCode ?? this.successCode;
    this.tokenInvalidCode = tokenInvalidCode ?? this.tokenInvalidCode;
    this.errorMessage = errorMessage ?? this.errorMessage;
    this.baseUrl = baseUrl ?? this.baseUrl;
    this.headerInterceptor = headerInterceptor ?? this.headerInterceptor;
    this.tokenInterceptor = tokenInterceptor ?? this.tokenInterceptor;
    this.logInterceptor = logInterceptor ?? this.logInterceptor;
    this.isNeedProxy = isNeedProxy ?? false;
    String proxy = proxyHost ?? "";
    this.proxyHost = "PROXY " + proxy;
    this.headers = headers ?? this.headers;
    this.interceptors = interceptors ?? this.interceptors;
  }
}
